package com.tsn.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tsn.common.Result;
import com.tsn.entity.Site;
import com.tsn.service.SiteService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 景点 前端控制器
 * </p>
 *
 * @author tsn
 * @since 2021-08-13
 */
@RestController
@RequestMapping("/tsn/site")
public class SiteController {
    @Autowired
    SiteService siteService;
//    @RequiresAuthentication
    @GetMapping("/getSites")
    public Result getSiteList(){
        return  Result.success(siteService.list());
    }
    @RequiresAuthentication
    @PostMapping("/addSite")
    public Result addSite(@RequestBody JSONObject article){
//        System.out.println(site);
        if(article==null)
            return Result.failure("数据异常，请重试");
        Site site = new Site();
        site.setSiteTitle(article.getString("title"));
        site.setSiteAuthor(article.getString("editor"));
        site.setContent(article.getString("content"));
        siteService.save(site);
        return Result.success(null);
    }
    @GetMapping("/getSiteOne")
    public  Result getSiteById(@RequestParam("id")Integer id){
        return  Result.success(siteService.getById(id));
    }
    @GetMapping("/getAttentionSites")
    public  Result getAttentionSites(@RequestParam("userId")Integer userId){
        return  siteService.listAttentionSites(userId);
    }
    @PutMapping("/updateSite")
    public Result updateSite(@RequestBody JSONObject object){
        Site site = JSON.toJavaObject(object, Site.class);
        siteService.updateById(site);
        return Result.success(null);
    }
    @PutMapping("/addCover")
    public Result addCover(@RequestBody JSONObject object){
        String cover = object.getString("cover");
        Integer id = object.getInteger("id");
        Site site = siteService.getById(id);
        site.setCover(cover);
        siteService.updateById(site);
        return Result.success(null);
    }

}

